home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Code Resources / Jims CDEFs 1.50 / CDEF Source / source / qdCDEF.c < prev    next >
Encoding:
Text File  |  1995-11-08  |  4.2 KB  |  140 lines  |  [TEXT/KAHL]

  1. //------------------------- © 1994-1995 by James G. Stout --------------------------
  2. // File        : qdCDEF.c
  3. // Purpose    : CDEF quickDraw routines for offScreen drawing and quickdraw version
  4. // Date        : 23 June 1992
  5. //
  6. //----------------------------------------------------------------------------------
  7.  
  8. #include <GestaltEqu.h>
  9. #include <Memory.h>
  10. #include <OSUtils.h>
  11. #include <QDOffscreen.h>
  12. #include <ToolUtils.h>
  13. #include <Traps.h>
  14. #include <Types.h>
  15. #include <Windows.h>
  16.  
  17. #include "miscCDEF.h"
  18. #include "qdCDEF.h"
  19.  
  20. #define NOPIXELS 0
  21.  
  22. //==================================================================================
  23. // Get an offscreen drawing port, either GWorld or Bitmap for specified localRect.
  24. // If 32 Bit ColorQuickdraw if found, we try for a GWorld, otherwise, just create
  25. // an offscreen bitmap.
  26. //
  27. // Returns : depth of offPort and a filled in GWorld or Bitmap
  28. //==================================================================================
  29.  
  30. extern short getOff(CGrafPtr * offPort, Rect * localRect)
  31. {
  32.     short        wid, ht;
  33.     Rect        globalRect;
  34.     BitMap        newBits;
  35.     QDErr        err;
  36.     GrafPtr        savePort;
  37.     RgnHandle    grayRgn = GetGrayRgn();
  38.     
  39. //----------------------------------------------------------------------------------
  40. // get an offscreen bitmap/GWorld to minimize flicker when drawing    
  41. //----------------------------------------------------------------------------------
  42.  
  43.     if(getQDVers() >= gestalt32BitQD12) {                // try to use a GWorld                
  44.         globalRect = *localRect;
  45.         LocalToGlobal((Point *)&globalRect.top);
  46.         LocalToGlobal((Point *)&globalRect.bottom);
  47.         
  48.         if(!RectInRgn(&globalRect, grayRgn))            // avoid crash with v1.2 cQD
  49.             return NOPIXELS;
  50.             
  51.         if(*offPort == 0) {                                // create a new GWorld                
  52.             err = NewGWorld((CGrafPtr *)offPort,0,&globalRect,0,0,0);
  53.         }
  54.         else {                                            // update existing GWorld            
  55.             err = UpdateGWorld((CGrafPtr *)offPort,0,&globalRect,0,0,0);
  56.         }
  57.         if(err == noErr)
  58.             return (**((*offPort)->portPixMap)).pixelSize;
  59.         return NOPIXELS;                                // something is seriously amiss…    
  60.     }
  61.     else
  62.     if(*offPort == 0) {                                    // create a bitmap                    
  63.         wid = (*localRect).right - (*localRect).left;
  64.         ht  = (*localRect).bottom - (*localRect).top;
  65.         SetRect(&newBits.bounds,0,0,wid,ht);
  66.         newBits.rowBytes = ((wid + 31)/32) * 4;
  67.         newBits.baseAddr = NewPtr(ht * newBits.rowBytes);
  68.         if(MemError())
  69.             return NOPIXELS;
  70.     
  71.         CopyBits(&newBits,&newBits,                        // clear the new bitmap                    
  72.                  &newBits.bounds,&newBits.bounds,
  73.                   srcXor,nil);
  74.                  
  75.         GetPort(&savePort);
  76.         *offPort = (CGrafPtr)NewPtr(sizeof(GrafPort));
  77.         if(MemError())
  78.             return NOPIXELS;    
  79.         OpenPort((GrafPtr)*offPort);
  80.         SetPort((GrafPtr)*offPort);
  81.         
  82.         (**offPort).portRect = newBits.bounds;
  83.         RectRgn((**offPort).visRgn,&newBits.bounds);
  84.         SetPortBits(&newBits);
  85.         SetPort(savePort);
  86.     }
  87.     return 1;
  88. }
  89.  
  90. //==================================================================================
  91. // Lock down the offPort pixels and return a locked handle to the locked pixels
  92. //==================================================================================
  93.  
  94. extern PixMapHandle getLockedPixels(CGrafPtr * offPort, short qdVers)
  95. {
  96.     PixMapHandle    pmHdl;
  97.     
  98.     if(qdVers >= gestalt32BitQD13) {                    // no bug in the
  99.         pmHdl = GetGWorldPixMap(*offPort);                // GetGWorldPixMap routine
  100.         if(!LockPixels(pmHdl)) {
  101.                 pmHdl = 0;
  102.         }
  103.     }
  104.     else
  105.         pmHdl = (*offPort)->portPixMap;
  106.     
  107.     if(pmHdl)
  108.         HLock((Handle)pmHdl);
  109.     return(pmHdl);
  110. }
  111.  
  112. //==================================================================================
  113. //    Unlock PixMapHandle & pixels
  114. //==================================================================================
  115. extern void unlockPixels(PixMapHandle pmHdl, short qdVers)
  116. {    
  117.     if(pmHdl) {
  118.         HUnlock((Handle)pmHdl);
  119.         if(qdVers >= gestalt32BitQD13) {                // no bug in the
  120.             UnlockPixels(pmHdl);                        // GetGWorldPixMap routine
  121.         }
  122.     }
  123. }
  124.  
  125. //==================================================================================
  126. //    Return QD version number from Gestalt 
  127. //==================================================================================
  128.  
  129. short getQDVers()        
  130. {
  131.     OSErr        err;
  132.     long        gResult;
  133.     
  134.     if(trapAvailable(_Gestalt)) {        
  135.         err = Gestalt(gestaltQuickdrawVersion,&gResult);
  136.         if(err == noErr)
  137.             return(LoWord(gResult));
  138.     }
  139.     return(gestaltOriginalQD);
  140. }